package com.xinyi.tangmi;

import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.xinyi.tangmi.R;

public class RegionAdapter extends BaseAdapter {

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	class ViewHolder {
		TextView usernameTextView, phoneTextView, addressTextView,
				detailAddressTextView,isDefault;
		ImageView editAddress, defaultAddress;
	}

	ListView listView;
	List<Map<String, String>> list;
	Handler handler;

	public RegionAdapter(List<Map<String, String>> list, Handler handler) {
		// TODO Auto-generated constructor stub
		this.list = list;
		this.handler = handler;
	}

	@SuppressLint("ViewHolder")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (listView == null) {
			listView = (ListView) parent;
		}
		if (convertView == null) {
			convertView = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.layout_address_manage_item, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.usernameTextView = (TextView) convertView
					.findViewById(R.id.tv_name);
			viewHolder.phoneTextView = (TextView) convertView
					.findViewById(R.id.tv_phone);
			viewHolder.addressTextView = (TextView) convertView
					.findViewById(R.id.tv_detail_01);
			viewHolder.detailAddressTextView = (TextView) convertView
					.findViewById(R.id.tv_detail_02);
			viewHolder.editAddress = (ImageView) convertView
					.findViewById(R.id.iv_address_edit);
			viewHolder.isDefault = (TextView) convertView
					.findViewById(R.id.isdefault);
			Map<String, String> map = list.get(position);
			viewHolder.usernameTextView.setText(map.get("consignee"));
			viewHolder.phoneTextView.setText(map.get("tel"));
			viewHolder.addressTextView.setText(map.get("province") + "-"
					+ map.get("city") + "-" + map.get("district"));
			viewHolder.detailAddressTextView.setText(map.get("address") + "("
					+ map.get("zipcode") + ")");
			if(map.get("is_default").equals("1"))
			{
				viewHolder.isDefault.setVisibility(View.VISIBLE);
			}else {
				viewHolder.isDefault.setVisibility(View.GONE);
			}
			viewHolder.editAddress.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Message message = new Message();
					message.what = AddressManageActivity.EDIT_ITEM;
					message.arg1 = position;
					handler.sendMessage(message);
				}
			});
		}
		return convertView;
	}
}
